package org.example.y24.m9;

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.Scanner;

public class Test3 extends JFrame {
    private static int size; // 棋盘大小
    private static Integer[][] arr; // 表示棋盘
    private static int count; // 填充计数器
    private static int specialRow, specialCol; // 特殊方块坐标

    private static final Color[] COLORS = new Color[]{
            Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA,
            Color.ORANGE, Color.PINK, Color.YELLOW, Color.GRAY,
            Color.LIGHT_GRAY, Color.DARK_GRAY, Color.WHITE
    };

    public Test3(int n) {
        size = (int) Math.pow(2, n);
        arr = new Integer[size][size];
        count = 0;
        Random random = new Random();
        specialRow = random.nextInt(size);
        specialCol = random.nextInt(size);
        System.out.println("特殊格子坐标=(" + (specialRow + 1) + "," + (specialCol + 1) + ")");
        arr[specialRow][specialCol] = 0; // 特殊格子标记为0
        Cover(0, 0, specialRow, specialCol, size);

        setTitle("L形骨牌覆盖");
        setSize(1200, 1200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示窗口
        setLayout(new BorderLayout());

        add(new ChessBoardPanel(), BorderLayout.CENTER);
        setVisible(true);
    }

    // 4. 定义解决问题的函数
    public static void Cover(int leftRow, int leftCol, int specialRow, int specialCol, int size) {
        if (size < 2) {
            return;
        }

        count++; // 增加计数器
        int flag = count; // 当前L形骨牌的编号

        // 如果特殊方块在左上子棋盘
        if (specialRow < leftRow + size / 2 && specialCol < leftCol + size / 2) {
            Cover(leftRow, leftCol, specialRow, specialCol, size / 2); // 递归覆盖该子棋盘
            // 放置L形骨牌来连接其他三个子棋盘
            arr[leftRow + size / 2 - 1][leftCol + size / 2] = flag; // 右上角
            arr[leftRow + size / 2][leftCol + size / 2 - 1] = flag; // 左下角
            arr[leftRow + size / 2][leftCol + size / 2] = flag; // 右下角
            // 递归覆盖剩余的三个子棋盘
            Cover(leftRow, leftCol + size / 2, leftRow + size / 2 - 1, leftCol + size / 2, size / 2); // 右上子棋盘
            Cover(leftRow + size / 2, leftCol, leftRow + size / 2, leftCol + size / 2 - 1, size / 2); // 左下子棋盘
            Cover(leftRow + size / 2, leftCol + size / 2, leftRow + size / 2, leftCol + size / 2, size / 2); // 右下子棋盘
        } else if (specialRow < leftRow + size / 2 && specialCol >= leftCol + size / 2) { // 如果特殊方块在右上子棋盘
            Cover(leftRow, leftCol + size / 2, specialRow, specialCol, size / 2);
            arr[leftRow + size / 2 - 1][leftCol + size / 2 - 1] = flag; // 左上角
            arr[leftRow + size / 2][leftCol + size / 2 - 1] = flag; // 左下角
            arr[leftRow + size / 2][leftCol + size / 2] = flag; // 右下角
            Cover(leftRow, leftCol, leftRow + size / 2 - 1, leftCol + size / 2 - 1, size / 2); // 左上子棋盘
            Cover(leftRow + size / 2, leftCol, leftRow + size / 2, leftCol + size / 2 - 1, size / 2); // 左下子棋盘
            Cover(leftRow + size / 2, leftCol + size / 2, leftRow + size / 2, leftCol + size / 2, size / 2); // 右下子棋盘
        } else if (specialRow >= leftRow + size / 2 && specialCol < leftCol + size / 2) { // 如果特殊方块在左下子棋盘
            Cover(leftRow + size / 2, leftCol, specialRow, specialCol, size / 2);
            arr[leftRow + size / 2 - 1][leftCol + size / 2 - 1] = flag; // 左上角
            arr[leftRow + size / 2 - 1][leftCol + size / 2] = flag; // 右上角
            arr[leftRow + size / 2][leftCol + size / 2] = flag; // 右下角
            Cover(leftRow, leftCol, leftRow + size / 2 - 1, leftCol + size / 2 - 1, size / 2); // 左上子棋盘
            Cover(leftRow, leftCol + size / 2, leftRow + size / 2 - 1, leftCol + size / 2, size / 2); // 右上子棋盘
            Cover(leftRow + size / 2, leftCol + size / 2, leftRow + size / 2, leftCol + size / 2, size / 2); // 右下子棋盘
        } else if (specialRow >= leftRow + size / 2 && specialCol >= leftCol + size / 2) { // 如果特殊方块在右下子棋盘
            Cover(leftRow + size / 2, leftCol + size / 2, specialRow, specialCol, size / 2);
            arr[leftRow + size / 2 - 1][leftCol + size / 2 - 1] = flag; // 左上角
            arr[leftRow + size / 2 - 1][leftCol + size / 2] = flag; // 右上角
            arr[leftRow + size / 2][leftCol + size / 2 - 1] = flag; // 左下角
            Cover(leftRow, leftCol + size / 2, leftRow + size / 2 - 1, leftCol + size / 2, size / 2); // 右上子棋盘
            Cover(leftRow + size / 2, leftCol, leftRow + size / 2, leftCol + size / 2 - 1, size / 2); // 左下子棋盘
            Cover(leftRow, leftCol, leftRow + size / 2 - 1, leftCol + size / 2 - 1, size / 2); // 左上子棋盘
        }
    }

    // 自定义面板用于绘制棋盘
    private class ChessBoardPanel extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int cellSize = Math.min(getWidth(), getHeight()) / size;
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    Color color;
                    if (i == specialRow && j == specialCol) {
                        color = Color.RED; // 特殊方块为红色
                    } else if (arr[i][j] != null && arr[i][j] > 0) {
                        color = COLORS[arr[i][j] % COLORS.length]; // 根据L形骨牌编号选择颜色
                    } else {
                        color = Color.WHITE; // 空白单元格为白色
                    }
                    g.setColor(color);
                    g.fillRect(j * cellSize, i * cellSize, cellSize, cellSize);
                    if (arr[i][j] != null && arr[i][j] > 0) {
                        g.setColor(Color.BLACK);
                        g.drawString(arr[i][j].toString(), j * cellSize + 2, (i + 1) * cellSize - 2);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = 1;
        while (n > 0){
            n = sc.nextInt();
            int k = n;
            EventQueue.invokeLater(() -> new Test3(k));
        }
    }
}